package niit.com;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

import static jdk.internal.org.jline.utils.Colors.s;

@WebServlet("/login")
public class Login extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String hiddenFieldValue = request.getParameter("hiddenField"); // 获取隐藏表单域的值
        // 输出隐藏表单域的值到控制台（仅用于调试）
        System.out.println("Hidden Field Value: " + hiddenFieldValue);
        boolean isAuthenticated = authenticateUser(username, password);

        if (username.equals("admin") && password.equals("123"))
        {
            response.sendRedirect("index.html");
            HttpSession session = request.getSession();
            System.out.println("LOGIN:"+session.getId());
            session.setAttribute("username", username);

            resp.sendRedirect(s:"index");
        }
        else
        {
            response.sendRedirect("Login.html?error=auth");
        }
    }

    private boolean authenticateUser(String username, String password) {
        // 直接判断用户名和密码，无需连接数据库
        return "root".equals(username) && "123456".equals(password);
    }
}